package com.andaily.domain.thing;

import com.andaily.domain.AbstractDomain;
import com.andaily.domain.dto.thing.ThingDto;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.user.User;
import com.andaily.web.context.BeanProvider;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * @author Shengzhao Li
 */
public class Thing extends AbstractDomain {

    private static ThingRepository thingRepository() {
        return BeanProvider.getBean(ThingRepository.class);
    }

    //内容
    private String content;
    //优先极
    private ThingPriority priority = ThingPriority.defaultPriority();
    //所属事件, 可选
    private Event event;
    //开始日期,如果不清楚开始时间,则为空; 因为我也不知道
    private Date startDate;

    private ThingStatus status = ThingStatus.CREATED;

    private User owner;

    private Date cancelTime;
    private Date endTime;

    //  Whether pending by scheduler
    private boolean autoPendingByScheduler;

    public Thing() {
    }

    public Thing(String content, ThingPriority priority, Date startDate, ThingStatus status) {
        this.content = content;
        this.priority = priority;
        this.startDate = startDate;
        this.status = status;
    }

    public String content() {
        return content;
    }

    public ThingPriority priority() {
        return priority;
    }

    public Event event() {
        return event;
    }

    public Date startDate() {
        return startDate;
    }

    public ThingStatus status() {
        return status;
    }

    public User owner() {
        return owner;
    }

    public Thing updateEvent(Event event) {
        this.event = event;
        return this;
    }

    public Thing update(ThingDto thingDto) {
        this.content = thingDto.getContent();
        this.priority = thingDto.getPriority();
        this.status = thingDto.getStatus();

        final String startDateAsText = thingDto.getStartDate();
        if (StringUtils.isNotEmpty(startDateAsText)) {
            this.startDate = DateUtils.getDate(startDateAsText);
        }
        final String eventGuid = thingDto.getEventGuid();
        if (StringUtils.isNotEmpty(eventGuid)) {
            this.updateEvent(thingRepository().findEventByGuid(eventGuid));
        }

        return this;
    }

    public Thing updateOwner(User owner) {
        this.owner = owner;
        return this;
    }

    public void saveOrUpdate() {
        if (isNewly()) {
            thingRepository().saveThing(this);
        } else {
            thingRepository().updateThing(this);
        }
    }

    public void updateStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date cancelTime() {
        return cancelTime;
    }

    public Date endTime() {
        return endTime;
    }

    public Thing updateAutoPendingByScheduler(boolean autoPendingByScheduler) {
        this.autoPendingByScheduler = autoPendingByScheduler;
        return this;
    }

    public boolean autoPendingByScheduler() {
        return autoPendingByScheduler;
    }
}
